package com.cdream.basic.algorithm.leetcode.string;

import java.util.ArrayList;
import java.util.List;

public class Question6 {
    public String convert(String s, int numRows) {
        if (numRows >= s.length() || numRows==1) {
            return s;
        }
        List<StringBuilder> sbs = new ArrayList<>();
        for (int i = 0; i < numRows; i++) {
            sbs.add(new StringBuilder());
        }
        int index = 0;
        int direct = -1;
        for (int i = 0; i < s.length(); i++) {
            StringBuilder sb = sbs.get(index);
            sb.append(s.charAt(i));
            if (index == 0) {
                direct = 1;
            }else if (index == numRows - 1) {
                direct = -1;
            }
            if (direct == 1) {
                index++;
            } else {
                index--;
            }

        }
        StringBuilder res = new StringBuilder();
        for (StringBuilder sb : sbs) {
            res.append(sb);
        }
        return res.toString();
    }

    public static void main(String[] args) {
        new Question6().convert("AB", 1);
    }
}
